package com.wangyadong.hobby.thread.lesson7withconcurrent;

import java.math.BigInteger;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.stream.IntStream;

public class PrimeProducer extends Thread {
    private final BlockingQueue<BigInteger> queue;

    public PrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    public void run() {
        try {
            BigInteger p = BigInteger.ONE;
            while (!Thread.currentThread().isInterrupted())
                queue.put(p = p.nextProbablePrime());
        } catch (InterruptedException consumed) {
            /* Allow thread to exit */
            consumed.printStackTrace();
        }
    }

    public void cancel() {
        interrupt();
    }


    public static void main(String[] args) throws InterruptedException {
        BlockingQueue queue = new ArrayBlockingQueue(100);
        PrimeProducer t1 = new PrimeProducer(queue);
        PrimeProducer t3 = new PrimeProducer(queue);
        PrimeProducer t2 = new PrimeProducer(queue);


        t1.start();
        t2.start();
        t3.start();
//        Thread thread = new Thread();
//        thread.start();
//
//        thread.interrupt();
//        t1.interrupt();
        t2.interrupt();
        System.out.println("11111");
        while (t2.queue.size() == 100){
            t2.interrupt();
        }

    }
}